<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class TopCategoryUsersWidget extends Widget
{

    /**
     *
     * @param $data = array('expire' => 30, 'limit'=> 10, 'cid' => 1);
     */
    public function render($data)
    {
        $expire = !empty($data['expire']) ? intval($data['expire'] * 60) : 60 * 60;
        $limit  = !empty($data['limit']) ? intval($data['limit']) : 10;
        $cateID = !empty($data['cid']) ? intval($data['cid']) : 0;

        $name = 'cache_top_category_users_'.$cateID;
        $users = S($name);

        if (empty($users) && $cateID)
        {
            $Model  = M();
        	$prefix = C('DB_PREFIX');
        	$cateIDs = get_sub_category_ids($cateID, 1);
        	$cateIDs = $cateIDs ? implode(',', $cateIDs) : 0;
            $where = " is_best=1 AND category_id IN ({$cateIDs}) ";

            $sql  = " SELECT user_id,username,email,point_count,COUNT(poster_id) AS best_answer_count ";
        	$sql .= " FROM {$prefix}answer A LEFT JOIN {$prefix}user U ON A.poster_id=U.user_id ";
        	$sql .= " WHERE {$where} ";
        	$sql .= " GROUP BY poster_id ORDER BY best_answer_count DESC LIMIT {$limit} ";

        	$users = $Model->query($sql);
        	S($name, $users, $expire);
        }
        $data['users'] = $users;

        $tplFile = TEMPLATE_PATH .'/Home/Widget_topCategoryUsers.html';
        $content = $this->renderFile($tplFile, $data);
        return $content;
    }

}

/* End of file HelpTipsWidget.class.php */
/* Location: ./App/Lib/Widget/HelpTipsWidget.class.php */
